package com.teach.service.impl;

import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;

import javax.annotation.PostConstruct;
import javax.imageio.ImageIO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.teach.config.ConfigProperties;
import com.teach.service.WatermarkService;

import lombok.extern.slf4j.Slf4j;

/**
 * <pre>
 * 基于配置的水印实现
 * 创建时间 ：	2019年12月11日 上午11:29:25
 * 当前版本：	V1.0
 * Copyright © 1998 - 2019 jlzhou.top. All Rights Reserved	
 * </pre>
 * @author J.L.Zhou
 *
 */
@Service
@Slf4j
public class WatermarkServiceImpl implements WatermarkService {
	
	private BufferedImage img;

	
	@Autowired
	private ConfigProperties config;
	
	
	@Override
	public BufferedImage getWatermark() {
		return img;
	}

	@PostConstruct
	@Override
	public void reload() {
		if(StringUtils.isEmpty(config.image.watermark)) {
			try {
				img = ImageIO.read(this.getClass().getResourceAsStream("/_w.png"));
			} catch (IOException e) {
				log.warn("获取默认水印失败",e);
			}
		}else if(config.image.watermark.indexOf("://")>-1) {
			try {
				img = ImageIO.read(new URL(config.image.watermark));
			} catch (IOException e) {
				log.warn("获取URL水印失败",e);
			}
		}else{
			try {
				img = ImageIO.read(new FileInputStream(config.image.watermark));
			} catch (IOException e) {
				log.warn("获取本地水印失败",e);
			}
		}
	}

}
